/* Cac ham su dung trong toan bo chuong trinh */

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include "DeclearFunc.c"
#include "struct.c"

/*--------------------KHAI BAO HANG---------------------------*/
const char LINK_ACCOUNT[255] = {".Account\\"};
const char LINK_LIBRARY[255] = {".Library\\"};
const char LINK_STATISTICS[255] = {".Statistics\\"};
const char LINK_CART[255] = {".Cart\\"};

/*---------------------PHAN DANG NHAP DUNG CHUNG-------------------*/
//=> Dang nhap
int valiLogin(char *ID, char *password) {
	FILE *f;
	char tempPW[255], tempID[255], link[255];
	int isValidFile = findFile(ID, LINK_ACCOUNT);
	strcpy(tempID, ID);
	pathFile(tempID);
	strcpy(link, LINK_ACCOUNT);
	strcat(link, tempID);
	if(isValidFile) {
		f = fopen(link, "r");
		fscanf(f, "%s\n", tempPW);
		if(strcmp(tempPW, password) == 0) {
			fclose(f);
			return 1;
		}
		return 0;
	}
	return 0;
}

//=> Kiem tra dinh dang email
int checkMail(char *mail, int flag) {
	int i = 0, j = 0, count1 = 0, count2 = 0;
	char str[255], systemMail[255] = {"bookstore@amazon.com"};
	strcpy(str, mail);
	if(strlen(str) == 0) {
		return 0;
	}
	switch(flag) {
		case 0:
			for( ; i < strlen(str) - 1; i++) {
				if(str[i] == '@') {
					++count1;
					break;
				}
			}
			if(i != 0) {
				for( ; i < strlen(str) - 1; i++) {
					count1 = 0;
					if(str[i] == '@') {
						++count1;
					}
					if(str[i] == '.') {
						++count2;
					}
				}
			} else {
				return 0;
			}
			if(count1 == 0 && count2 != 0) {
				return 1;
			}
			return 0;
		case 1:
			if(strcmp(str, systemMail) == 0) {
				return 1;
			}
			return 0;
	}
}

//=> Kiem tra mat khau
int checkPW(char *password, int mode) {
	char systemPW[15] = {"bookstore"};
	switch(mode) {
		case 0:
			if(strlen(password) >= 6) {
				return 1;
			}
			return 0;
		case 1:
			if(strcmp(password, systemPW) == 0) {
				return 1;
			}
			return 0;
	}
}

/*------------------------CAC HAM XU LY FILE---------------------*/

//=> Kiem tra su ton tai cua file
int findFile(char *str, const char* folder) {
	FILE *f;
	char link[255], tempFile[255];
	strcpy(tempFile, str);
	deleteSpace(tempFile);
	pathFile(tempFile);
	strcpy(link, folder);
	strcat(link, tempFile);
	if((f = fopen(link, "r")) != NULL) {
		fclose(f);
		return 1;
	}
	fclose(f);
	return 0;
}

//=> Chuan hoa duong dan file
void pathFile(char *str) {
	char temp[255];
	strcpy(temp, str);
	strcat(temp, ".amazon");
	strcpy(str, temp);
}

/*----------------THANH TOAN VA THONG KE DOANH THU---------------*/
//=> Khach thanh toan sach trong gio hang
void statisticBook(char* nameOfBook, long int price) {
	FILE *f;
	char fileSt[280], link[255], tempNameBook[255];
	long int temp;
	linkStatistics(link);
	strcpy(fileSt, link);
	strcpy(tempNameBook, nameOfBook);
	deleteSpace(tempNameBook);
	pathFile(tempNameBook);
	strcat(fileSt, tempNameBook);
	f = fopen(fileSt, "r");
	fscanf(f, "%d", &temp);
	fclose(f);
	temp += price;
	f = fopen(fileSt, "w");
	fprintf(f, "%d", temp);
	fclose(f);
}

//=> Tao duong link ngay thang nam
void linkStatistics(char* c) {
	time_t now;
	time(&now);
	char *year, *month, *day;
	year = (char*) malloc(sizeof(year));
	month = (char*) malloc(sizeof(month));
	day = (char*) malloc(sizeof(day));
	struct tm*info;
	info = localtime(&now);
	strftime(year, 5, "%Y", info);
	strftime(month, 4, "%m", info);
	strftime(day, 4, "%d", info);
	char link[280];
	strcat(link, LINK_STATISTICS);
	strcat(link, year);
	strcat(link, "\\");
	strcat(link, month);
	strcat(link, "\\");
	strcat(link, day);
	strcat(link, "\\");
	free(day);
	free(month);
	free(year);
	strcpy(c, link);
}

//=> Thong ke doanh thu
unsigned long int statisticsRevenue(char* yearSt, char* monthSt, char* daySt, int choose) {
	FILE *f1, *f2;
	bookInfo bookInfo[100];
	char link[255], linkDay[255], managerLink[255], linkMonth[255],
		 linkBook[255], c[3], month[3], day[3], tempNameBook[255], tempDay[3], tempMonth[3];
	unsigned long int sum = 0, temp = 0;
	int i = 1, j, k = 0, sumBook;
	if(strlen(daySt) == 1) {
		strcat(tempDay, "0");
		strcat(tempDay, daySt);
		strcpy(daySt, tempDay);
	}
	if(strlen(monthSt) == 1) {
		strcat(tempMonth, "0");
		strcat(tempMonth, monthSt);
		strcpy(monthSt, tempMonth);
	}
	switch(choose) {
		//=> Doanh thu theo nam
		case 1:
			strcpy(link, LINK_STATISTICS);
			strcat(link, yearSt);
			strcat(link, "\\");

			strcpy(managerLink, LINK_LIBRARY);
			strcat(managerLink, "Books.amazon");
			f1 = fopen(managerLink, "r+");
			fscanf(f1, "%d", &sumBook);

			for(i = 1 ; i <= 1; ++i) {
				if(i < 10) {
					itoa(i, c, 10);
  					strcpy(month, "0");
					strcat(month, c);
				} else {
					itoa(i, month, 10);
				}
				strcpy(linkMonth, link);
				strcat(linkMonth, month);
				strcat(linkMonth, "\\");
				for(j = 1 ; j <= 1; ++j) {
					if(j < 10) {
						itoa(j, c, 10);
						strcpy(day, "0");
						strcat(day, c);
					} else {
						itoa(j, day, 10);
					}
					strcpy(linkDay, linkMonth);
					strcat(linkDay, day);
					strcat(linkDay, "\\");
					k = 0;
					//=> Doc file sach vao struct
					while(k < sumBook) {
						temp = 0;
						fscanf(f1, "\n%s", bookInfo[k].name);
						strcpy(tempNameBook, bookInfo[k].name);
						pathFile(tempNameBook);
						strcpy(linkBook, linkDay);
						strcat(linkBook, tempNameBook);
						if((f2 = fopen(linkBook, "r+")) != NULL) {
							fscanf(f2, "%d", &temp);
							sum += temp;
						}
						k++;
					}
				}
			}
			break;
		//=> Doanh thu theo thang
		case 2:
			strcpy(link, LINK_STATISTICS);
			strcat(link, yearSt);
			strcat(link, "\\");
			strcat(link, monthSt);
			strcat(link, "\\");

			strcpy(managerLink, LINK_LIBRARY);
			strcat(managerLink, "Books.amazon");
			for(j = 1 ; j <= 30; ++j) {
				if(j < 10) {
					itoa(j, c, 10);
					strcpy(day, "0");
					strcat(day, c);
				} else {
					itoa(j, day, 10);
				}
				strcpy(linkDay, link);
				strcat(linkDay, day);
				strcat(linkDay, "\\");
				k = 0;
				f1 = fopen(managerLink, "r+");
				fscanf(f1, "%d", &sumBook);
				//=> Doc file sach vao struct
				while(k < sumBook) {
					temp = 0;
					fscanf(f1, "\n%s", bookInfo[k].name);
					strcpy(tempNameBook, bookInfo[k].name);
					pathFile(tempNameBook);
					strcpy(linkBook, linkDay);
					strcat(linkBook, tempNameBook);
					if((f2 = fopen(linkBook, "r+")) != NULL) {
						fscanf(f2, "%d", &temp);
						sum += temp;
					}
					k++;
				}
			}
			break;
		//=> Doanh thu theo ngay
		case 3:
			strcpy(link, LINK_STATISTICS);
			strcat(link, yearSt);
			strcat(link, "\\");
			strcat(link, monthSt);
			strcat(link, "\\");
			strcat(link, daySt);
			strcat(link, "\\");

			strcpy(managerLink, LINK_LIBRARY);
			strcat(managerLink, "Books.amazon");

			f1 = fopen(managerLink, "r+");
			fscanf(f1, "%d", &sumBook);
			while(k < sumBook) {
				temp = 0;
				fscanf(f1, "\n%s", bookInfo[k].name);
				strcpy(tempNameBook, bookInfo[k].name);
				pathFile(tempNameBook);
				strcpy(linkBook, link);
				strcat(linkBook, tempNameBook);
				if((f2 = fopen(linkBook, "r+")) != NULL) {
					fscanf(f2, "%d", &temp);
					sum += temp;
				}
				k++;
			}
			break;
	}
	fclose(f1);
	fclose(f2);
 	return sum;
}

/* --------------------QUAN LY THONG TIN KHACH HANG--------------*/
//=> Tao tai khoan
int addUser(char *name, char *address, char *phone, char *age, char *sex,
			char *bankAccount, char *job, char *email, char *password) {
	FILE *f;
	char str[255], tempEmail[255];
	int isValidMail = checkMail(email, 0), lengthBankAcc = strlen(bankAccount),
		isValidPW = checkPW(password, 0);
	strcpy(tempEmail, email);
	deleteSpace(tempEmail);
	int isValidFile = findFile(email, LINK_ACCOUNT);
	pathFile(tempEmail);
	strcpy(str, LINK_ACCOUNT);
	strcat(str, tempEmail);
	if(isValidMail && !isValidFile && isValidPW && lengthBankAcc == 14) {
		f = fopen(str, "w");
		fprintf(f, "%s\n", password);
		deleteSpace(name);
		fprintf(f, "%s\n", name);
		fprintf(f, "%s\n", age);
		fprintf(f, "%s\n", sex);
		deleteSpace(address);
		fprintf(f, "%s\n", address);
		fprintf(f, "%s\n", phone);
		deleteSpace(job);
		fprintf(f, "%s\n", job);
		fprintf(f, "%s\n", bankAccount);
  		fclose(f);
		return 1;
	}
	fclose(f);
	return 0;
}

//=> Sua thong tin khach hang
int editUser(char *name, char *address, char *phone, char *age, char *sex, char *job,
			 char *email, char *bankAccount, char *password) {
	FILE *f;
	char str[255], tempEmail[255];
	int isValidFile = findFile(email, LINK_ACCOUNT);
	strcpy(tempEmail, email);
	deleteSpace(tempEmail);
	pathFile(tempEmail);
	strcpy(str, LINK_ACCOUNT);
	strcat(str, tempEmail);
	if(isValidFile) {
		f = fopen(str, "w");
		fprintf(f, "%s\n", password);
		deleteSpace(name);
		fprintf(f, "%s\n", name);
		fprintf(f, "%s\n", age);
		fprintf(f, "%s\n", sex);
		deleteSpace(address);
		fprintf(f, "%s\n", address);
		fprintf(f, "%s\n", phone);
		deleteSpace(job);
		fprintf(f, "%s\n", job);
		fprintf(f, "%s\n", bankAccount);
	  	fclose(f);
		return 1;
	}
	return 0;
}

//=> Tim kiem va in thong tin khach hang
void showUser(char* email) {
	customerInfo customerInfo;
	FILE *f;
	char str[255], tempEmail[255];
	int isValidMail = findFile(email, LINK_ACCOUNT);
	strcpy(tempEmail, email);
	deleteSpace(tempEmail);
	pathFile(tempEmail);
	strcpy(str, LINK_ACCOUNT);
	strcat(str, tempEmail);
	if(isValidMail) {
		printf("\t+=================================================+\n\n");
		printf("\t\tThong tin ve tai khoan %s\n\n", email);
		f = fopen(str, "r");
		//=> Password
		fscanf(f, "%s\n", customerInfo.password);
		//=> Ho ten
		fscanf(f, "%s\n", customerInfo.name);
		addSpace(customerInfo.name);
		printf("\tHo Ten: %s\n\n", customerInfo.name);
		//=> Tuoi
		fscanf(f, "%s\n", customerInfo.age);
		printf("\tTuoi: %s\t\t", customerInfo.age);
		//=> Gioi tinh
		fscanf(f, "%s\n", customerInfo.sex);
		printf("Gioi Tinh: %s\n\n", customerInfo.sex);
		//=> Dia chi
		fscanf(f, "%s\n", customerInfo.address);
		addSpace(customerInfo.address);
		printf("\tDia Chi: %s\n\n", customerInfo.address);
		//=> So dien thoai
		fscanf(f, "%s\n", customerInfo.phone);
		printf("\tSo Dien Thoai: %s\n\n", customerInfo.phone);
		//=> Nghe nghiep
		fscanf(f, "%s\n", customerInfo.job);
		addSpace(customerInfo.job);
		printf("\tNghe Nghiep: %s\n\n", customerInfo.job);
		//=> Tai khoan ngan hang
		fscanf(f, "%s\n", customerInfo.bankAccount);
		printf("\tSo Tai Khoan Ngan Hang: %s\n\n", customerInfo.bankAccount);
		printf("\t+=================================================+\n\n");
		flushCache(2);
		fclose(f);
	} else {
		printf("\n\t\t\t+===========================+\n\n");
		printf("\t\t\tKhong tim thay khach hang!\n\n");
		printf("\t\t\t+===========================+\n\n");
		flushCache(2);
		}
}

/*------------------QUAN LY THONG TIN SACH-----------------------*/
//=> Sua thong tin sach
int editBook(char *nameOfBook, char *author, char *publisher, int releaseYear, int price) {
	FILE *f;
	char str[255], tempNameBook[255];
	int isValidFile = findFile(nameOfBook, LINK_LIBRARY);
	strcpy(tempNameBook, nameOfBook);
	deleteSpace(tempNameBook);
	pathFile(tempNameBook);
	strcpy(str, LINK_LIBRARY);
	strcat(str, tempNameBook);
	if(isValidFile) {
		f = fopen(str, "w");
		deleteSpace(author);
		fprintf(f, "%s\n", author);
		deleteSpace(publisher);
		fprintf(f, "%s\n", publisher);
		fprintf(f, "%d\n", releaseYear);
		fprintf(f, "%d\n", price);
		fclose(f);
		return 1;
	}
	return 0;
}

//=> Them sach
int addBook(char *nameOfBook, char *author, char *publisher, int releaseYear, int price) {
	FILE *f;
	char str[255], tempNameBook[255];
	strcpy(tempNameBook, nameOfBook);
	deleteSpace(tempNameBook);
	int isValidFile = findFile(tempNameBook, LINK_LIBRARY);
	pathFile(tempNameBook);
	strcpy(str, LINK_LIBRARY);
	strcat(str, tempNameBook);
	if(!isValidFile) {
		f = fopen(str, "w");
		deleteSpace(author);
		fprintf(f, "%s\n", author);
		deleteSpace(publisher);
		fprintf(f, "%s\n", publisher);
		fprintf(f, "%d\n", releaseYear);
		fprintf(f, "%d\n", price);
		fclose(f);
		enterManagerBooks(nameOfBook, 1);
		return 1;
	}
	return 0;
}

//=> Xoa sach
int removeBook(char *nameOfBook) {
	char tempNameBook[255], link[255];
	strcpy(tempNameBook, nameOfBook);
	deleteSpace(tempNameBook);
	pathFile(tempNameBook);
	strcpy(link, LINK_LIBRARY);
	strcat(link, tempNameBook);
	if(remove(link)) {
 		return 0;
	}
	enterManagerBooks(nameOfBook, 0);
	return 1;
}

//=> Quan ly dau sach
void enterManagerBooks(char* nameOfBook, int choose) {
	FILE *f;
	bookInfo bookInfo[100];
	int i = 0, j, sumBook = 0;
	char tempNameBook[255];
	strcpy(tempNameBook, nameOfBook);
	deleteSpace(tempNameBook);
	//=> Doc file sach vao struct
	f = fopen("Library\\Books.amazon", "r+");
	fscanf(f, "%d", &sumBook);
	while(i < sumBook) {
		fscanf(f, "\n%s", bookInfo[i].name);
		i++;
	}
	fclose(f);
	switch(choose) {
		case 1:
			//=> Them sach
			f = fopen("Library\\Books.amazon", "w+");
			fprintf(f, "%d", sumBook + 1);
			strcpy(bookInfo[sumBook].name, tempNameBook);
			for(i = 0; i <= sumBook; i++) {
				fprintf(f, "\n%s", bookInfo[i].name);
			}
			fclose(f);
			break;
		case 0:
			//=> Xoa sach
			f = fopen("Library\\Books.amazon", "w");
			for(i = 0; i < sumBook; i++) {
				if(strcmp(bookInfo[i].name, tempNameBook) == 0) {
					for(j = i; j < sumBook - 1; j++) {
						strcpy(bookInfo[j].name, bookInfo[j + 1].name);
					}
					break;
				}
			}
			fprintf(f, "%d", --sumBook);
			for(i = 0; i < sumBook; i++){
				fprintf(f, "\n%s", bookInfo[i].name);
			}
			fclose(f);
			break;
	}
}

//=> Tim kiem va in thong tin ve cuon sach
void showOneBook(char* nameOfBook) {
	FILE *f;
	bookInfo bookInfo;
	char str[255], tempNameBook[255];
	int isValidBook = findFile(nameOfBook, LINK_LIBRARY);
	strcpy(tempNameBook, nameOfBook);
	deleteSpace(tempNameBook);
	pathFile(tempNameBook);
	strcpy(str, LINK_LIBRARY);
	strcat(str, tempNameBook);
	if(isValidBook) {
		f = fopen(str, "r");
		printf("\t+=================================================+\n\n");
		printf("\tTen sach: %s\n\n", nameOfBook);

		fscanf(f, "%s\n", bookInfo.author);
		addSpace(bookInfo.author);
		printf("\tTac gia: %s\n\n", bookInfo.author);

		fscanf(f, "%s\n", bookInfo.publisher);
		addSpace(bookInfo.publisher);
		printf("\tNXB: %s\n\n", bookInfo.publisher);

		fscanf(f, "%d\n", &bookInfo.releaseYear);
		printf("\tNam xuat ban: %d\n\n", bookInfo.releaseYear);

		fscanf(f, "%d", &bookInfo.price);
		printf("\tGia: %d VND\n\n", bookInfo.price);
		printf("\t+=================================================+");
		fclose(f);
		fflush(stdin);
	} else {
		printf("\t\tKhong tim thay sach!");
	}
	fflush(stdin);
}

//=> Cho sach vao gio
int addCart(char *nameOfBook, char *email) {
	FILE *f1, *f2;
	bookInfo bookInfo;
	char tempNameBook[255], tempOfMail[255], linkCart[255], linkLib[255];

	strcpy(tempNameBook, nameOfBook);
	deleteSpace(tempNameBook);
	pathFile(tempNameBook);
	strcpy(tempOfMail, email);
	strcpy(linkCart, LINK_CART);
	strcat(linkCart, tempOfMail);
	strcat(linkCart, tempNameBook);

	strcpy(linkLib, LINK_LIBRARY);
	strcat(linkLib, tempNameBook);
 //=> Doc thong tin sach
	f1 = fopen(linkLib, "r");
	fscanf(f1, "%s\n", bookInfo.author);
	fscanf(f1, "%s\n", bookInfo.publisher);
	fscanf(f1, "%d\n", &bookInfo.releaseYear);
	fscanf(f1, "%d", &bookInfo.price);
	//=> Cho sach vao gio
	if((f2 = fopen(linkCart, "r")) != NULL) {
		fclose(f1);
		fclose(f2);
		return 0;
	} else {
		fclose(f2);
		f2 = fopen(linkCart, "w");
		strcpy(bookInfo.name, nameOfBook);
		deleteSpace(bookInfo.name);
		fprintf(f2, "%s", bookInfo.name);
		fprintf(f2, "\n%s", bookInfo.author);
		fprintf(f2, "\n%s", bookInfo.publisher);
		fprintf(f2, "\n%d", bookInfo.releaseYear);
		fprintf(f2, "\n%d", bookInfo.price);
		fclose(f1);
		fclose(f2);
		return 1;
	}
}

/*----------------------- CAC HAM PHU----------------*/
//=> Xoa man hinh
void cls() {
	system("cls");
}

//=> Xoa bo nho dem
void flushCache(int mode) {
	switch(mode) {
		case 2:
			fflush(stdin);
			getch();
			break;
		case 1:
			fflush(stdin);
		case 0:
			getch();
	}
}

//=> Chuan hoa chuoi luu vao file (xoa khoang trang giua cac chu)
void deleteSpace(char *str) {
	char temp[255];
	strcpy(temp, str);
	int i = 0, j, length = strlen(temp);
	for( ; i < strlen(temp); i++) {
		if(temp[i] == ' ') {
			for(j = i; j < strlen(temp); j++) {
				temp[j] = temp[j + 1];
			}
			temp[length--] = '\0';
			i--;
		}
	}
	temp[strlen(temp) + 1] = '\0';
	strcpy(str, temp);
}

//=> Chuan hoa chuoi doc tu file (tao khoang trang)
void addSpace(char* str) {
	char temp[255];
	strcpy(temp, str);
	int length = strlen(temp);
	int i = 1, j;
	for( ; i <= strlen(temp); i++) {
		if(temp[i] >= 65 && temp[i] <= 90) {
			for(j = length; j > i; j--) {
				temp[j] = temp[j - 1];
			}
			temp[i] = ' ';
			temp[++length] = '\0';
			i++;
		}
	}
	strcpy(str, temp);
}

